//
//  RGUserInfoHeaderView.m
//  RedGrass
//
//  Created by wukong on 2018/11/27.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGUserInfoHeaderView.h"
#import <UIImageView+WebCache.h>


@implementation RGMeTagView

- (instancetype)initWithIcon:(UIImage *)image WithTitle:(NSString *)title {
    if (self = [super init]) {
        
//        self.backgroundColor = UICOLOR_WITH_RGBINT(0x160f2e);
        self.backgroundColor = [UIColor clearColor];
//        self.layer.cornerRadius = 4;
        
        [self addSubview:self.icon];
        [self addSubview:self.contentLabel];
        
        _contentLabel.text = title;
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.top.mas_equalTo(2);
            make.bottom.mas_equalTo(-2);
            make.trailing.mas_equalTo(-5);
            make.height.mas_equalTo(11);
        }];
        
        _icon.image = image;
        [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(image?5:0);
            make.centerY.mas_equalTo(0);
            make.size.mas_equalTo(image?CGSizeMake(9, 9):CGSizeMake(0, 0));
            make.trailing.equalTo(self.contentLabel.mas_leading).offset(-5);
        }];
    }
    return self;
}

-(UIImageView *)icon{
    if(!_icon){
        _icon = [[UIImageView alloc]init];
    }
    return _icon;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [[UILabel alloc]init];
        _contentLabel.textColor = [UIColor whiteColor];
        _contentLabel.font = FONT(customFontNameLight, 11);
    }
    return _contentLabel;
}
@end

@interface RGUserInfoHeaderView()<OnTabTapActionDelegate>

@property (strong, nonatomic) UIView *containerView;
@property (strong, nonatomic) UIImageView *bgImageView;
@property (strong, nonatomic) UIImageView *userIcon;
@property (strong, nonatomic) UILabel *nickName;

@property (strong, nonatomic) RGMeTagView *sexTag;
@property (strong, nonatomic) RGMeTagView *locationTag;
@property (strong, nonatomic) RGMeTagView *schoolTag;

@property (strong, nonatomic) UILabel *fansCount;
@property (strong, nonatomic) UILabel *favoriteCount;
@property (strong, nonatomic) UILabel *focusCount;
@property (strong, nonatomic) UILabel *desLabel;

@property (nonatomic,strong) UIView *btnsView;
@property (nonatomic,strong) UIButton *addFriendBtn;
@property (nonatomic,strong) UIButton *vipBtn;
@property (nonatomic,strong) UIButton *qRCodeBtn;

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIVisualEffectView *visualEffectView;
@property (nonatomic, assign) CGRect        bgImgFrame;
@end

@implementation RGUserInfoHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
//        [self addSubview:self.containerView];
//        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.mas_equalTo(UIEdgeInsetsZero);
//        }];
//        _containerView.layer.masksToBounds = YES;
        
//        [self addSubview:self.slideTabBar];
//        [_slideTabBar mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.and.right.mas_equalTo(self).mas_offset(0);
//            make.bottom.mas_equalTo(self).mas_offset(0);
//            make.height.mas_equalTo(45);
//        }];
        
        [self initUI];
        self.bgImgFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        self.visualEffectView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        self.bgImageView.frame = self.bgImgFrame;
        [self addMasonry];
        self.btnsView.hidden = NO;
       
    }
    return self;
}

-(NSString *)makeBirthDayWithDateStr:(NSString *)dateStr{
    if([dateStr length] == 0){
        return @"0岁";
    }
    NSTimeInterval time1 = [dateStr integerValue] / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];//创建一个日期格式化器
    dateFormatter.dateFormat=@"YYYY";
    NSInteger oldYear = [[dateFormatter stringFromDate:date] integerValue];
    NSInteger newYear = [[dateFormatter stringFromDate:[NSDate date]] integerValue];
    NSInteger year = newYear - oldYear >= 0 ? (newYear - oldYear):0;
    return [NSString stringWithFormat:@"%ld岁",year];
}

- (void)setModel:(RGUserInfoModel *)model {
    _model = model;
//    if (model.ID == [RGUserInfoModel getUserInfo].ID) {
//        self.btnsView.hidden = NO;
//    }else {
//        self.btnsView.hidden = YES;
//    }
//    [_bgImageView setImageWithURL:[NSURL URLWithString:model.image] placeholder:IMAGE_NAMED(@"个人主页_头像")];//IMAGE_NAMED(model.image);
//    [_userIcon setImageWithURL:[NSURL URLWithString:model.image] placeholder:IMAGE_NAMED(@"个人主页_头像")];//IMAGE_NAMED(model.image);
    [_bgImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:IMAGE_NAMED(@"个人主页_头像")];
    [_userIcon sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:IMAGE_NAMED(@"个人主页_头像")];
    _nickName.text = model.nickname;
    if(model.sex == 1 || model.sex == 2){
        _sexTag.icon.hidden = NO;
        _sexTag.icon.image = (model.sex == 1) ? IMAGE_NAMED(@"性别-男") : IMAGE_NAMED(@"性别-女");
        _sexTag.contentLabel.text = [self makeBirthDayWithDateStr:model.birthday];
    }else{
        _sexTag.icon.hidden = YES;
        _sexTag.contentLabel.text = [self makeBirthDayWithDateStr:model.birthday];
    }
    
    
    if (model.locality.length > 0) {
        _locationTag.contentLabel.text = model.locality;
        _locationTag.hidden = NO;
    }else {
        _locationTag.hidden = YES;
    }
//    if (model.schoolName.length > 0) {
//        _schoolTag.contentLabel.text = model.schoolName;
//    }else {
        _schoolTag.contentLabel.text = @"   ";
        _schoolTag.hidden = YES;
//    }
    _fansCount.text = [NSString stringWithFormat:@"%ld粉丝",(long)model.fansCounts];
    _focusCount.text = [NSString stringWithFormat:@"%ld关注",(long)model.followCounts];
    _favoriteCount.text = [NSString stringWithFormat:@"%ld赞",(long)model.likeCounts];
    _desLabel.text = model.userComment;
//    [_slideTabBar setLabels:@[[NSString stringWithFormat:@"%ld作品",(long)model.worksCount],[NSString stringWithFormat:@"%d动态",model.dynamicCount],[NSString stringWithFormat:@"%d喜欢",model.likeVideoCount]] tabIndex:self.tabIndex];
}

-(void) setIsShowBtnView:(BOOL)isShowBtnView{
    _isShowBtnView = isShowBtnView;
    if(isShowBtnView){
        _btnsView.hidden = NO;
    }else{
        _btnsView.hidden = YES;
    }
}

-(void)initUI{
    [self addSubview:self.bgImageView];
    [self addSubview:self.visualEffectView];
    [self addSubview:self.userIcon];
    [self addSubview:self.nickName];
    [self addSubview:self.fansCount];
    [self addSubview:self.focusCount];
    [self addSubview:self.favoriteCount];
    [self addSubview:self.sexTag];
    [self addSubview:self.locationTag];
    [self addSubview:self.schoolTag];
    
    [self addSubview:self.desLabel];
    [self addSubview:self.backView];
    [self addSubview:self.btnsView];
    [self.btnsView addSubview:self.qRCodeBtn];
//    [self.btnsView addSubview:self.vipBtn];
    [self.btnsView addSubview:self.addFriendBtn];

}

-(void)addMasonry{
    
//    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(UIEdgeInsetsZero);
//    }];
//    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//    visualEffectView.frame = self.bgImgFrame;
//    visualEffectView.alpha = 0.8;
//    [_bgImageView addSubview:self.visualEffectView];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [_btnsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.size.mas_equalTo(CGSizeMake(39*3+8*2, 39));
        make.trailing.mas_equalTo(0);
    }];
    
    [_qRCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(39, 39));
    }];
    
//    [_vipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.equalTo(self.qRCodeBtn.mas_leading).offset(-8);
//        make.top.equalTo(self.qRCodeBtn.mas_top).offset(0);
//        make.size.mas_equalTo(CGSizeMake(39, 39));
//    }];
    
    [_addFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.trailing.equalTo(self.qRCodeBtn.mas_leading).offset(-8);
//        make.trailing.equalTo(self.vipBtn.mas_leading).offset(-8);
        make.top.equalTo(self.qRCodeBtn.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(39, 39));
    }];
    
    [_userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.top.equalTo(self.qRCodeBtn.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self).mas_offset(0);
    }];
    
    [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userIcon.mas_bottom).mas_offset(8);
        make.centerX.mas_equalTo(self).mas_offset(0);
    }];
    
    [_fansCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nickName.mas_bottom).mas_offset(12);
        make.centerX.mas_equalTo(self).mas_offset(0);
    }];
    
    [_focusCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.fansCount.mas_left).mas_offset(-8);
        make.centerY.mas_equalTo(self.fansCount.mas_centerY).mas_offset(0);
    }];
    
    [_favoriteCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.fansCount.mas_right).mas_offset(8);
        make.centerY.mas_equalTo(self.fansCount.mas_centerY).mas_offset(0);
    }];
    
    [_locationTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.fansCount.mas_bottom).offset(10);
        make.centerX.mas_equalTo(20);
    }];
    
    [_sexTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.locationTag.mas_leading).offset(-5);
        make.centerY.equalTo(self.locationTag.mas_centerY).offset(0);
    }];

    [_schoolTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.locationTag.mas_trailing).offset(5);
        make.centerY.equalTo(self.locationTag.mas_centerY).offset(0);
    }];
    
    [_desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.locationTag.mas_bottom).mas_offset(10);
//        make.centerX.mas_equalTo(self).mas_offset(0);
        make.leading.mas_equalTo(self).mas_offset(15);
        make.width.mas_equalTo(KScreenWidth - 30);
    }];
}

- (void)actionBtnClick:(UIButton *)btn {
    if (self.actionBtnClick) {
        self.actionBtnClick(btn.tag);
    }
}

- (void)onTapAction:(UITapGestureRecognizer *)sender {
    if(self.delegate) {
        [self.delegate onUserActionTap:sender.view.tag];
    }
}

- (void)overScrollAction:(CGFloat) offsetY {
//    CGFloat scaleRatio = fabs(offsetY)/330.0f;
//    CGFloat overScaleHeight = (330.0f * scaleRatio)/2;
//    _bgImageView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(scaleRatio + 1.0f, scaleRatio + 1.0f), CGAffineTransformMakeTranslation(0, -overScaleHeight));
//    NSLog(@"scaleRatio *%f",scaleRatio);
}

- (void)scrollToTopAction:(CGFloat) offsetY {
//    CGFloat alphaRatio = offsetY/(330.0f - 44 - kStatusBarHeight);
//    NSLog(@"ratio + %f",alphaRatio);
//    _containerView.alpha =  alphaRatio;
//    _backView.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000,alphaRatio);
}

-(void)scrollViewDidScroll:(CGFloat)offsetY withAlpha:(CGFloat)alpha {
    CGRect frame = self.bgImgFrame;
    // 上下放大
    frame.size.height -= offsetY;
    frame.origin.y = offsetY;
    
    // 左右放大
    if (offsetY <= 0) {
        frame.size.width = frame.size.height * self.bgImgFrame.size.width / self.bgImgFrame.size.height;
        frame.origin.x   = (self.frame.size.width - frame.size.width) / 2;
    }
    _backView.alpha = alpha;
    self.bgImageView.frame = frame;
    self.visualEffectView.frame = frame;
}

#pragma mark - getter

-(UIVisualEffectView *)visualEffectView {
    if (!_visualEffectView) {
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _visualEffectView.frame = self.bgImgFrame;
        _visualEffectView.alpha = 0.7;
    }
    return _visualEffectView;
}

-(UIView *)containerView{
    if(!_containerView){
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0);
    }
    return _containerView;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.contentMode = UIViewContentModeScaleToFill;
        _bgImageView.image = IMAGE_NAMED(@"个人主页_头像");
        
    }
    return _bgImageView;
}

//-(SlideTabBar *)slideTabBar{
//    if(!_slideTabBar){
//        _slideTabBar = [[SlideTabBar alloc]init];
//        [_slideTabBar setLabels:@[@"0作品",@"0动态",@"0喜欢"] tabIndex:0];
//        _slideTabBar.delegate = self;
//    }
//    return _slideTabBar;
//}

-(UIImageView *)userIcon{
    if(!_userIcon){
        _userIcon = [[UIImageView alloc]init];
//        NSString *image = [RGUserInfoModel getUserInfo].image;
//        [_userIcon sd_setImageWithURL:[NSURL URLWithString:image]];
//        [_userIcon setImageWithURL:[NSURL URLWithString:image]];
//        _userIcon.image = IMAGE_NAMED(@"个人主页_头像");
        ViewRadius(_userIcon, 4);
    }
    return _userIcon;
}

-(UILabel *)nickName{
    if(!_nickName){
        _nickName = [[UILabel alloc]init];
        _nickName.textColor = [UIColor whiteColor];
        _nickName.font = FONT(customBoldFontName, 21);
//        _nickName.text = @"昵称";
    }
    return _nickName;
}

-(UILabel *)fansCount{
    if(!_fansCount){
        _fansCount = [[UILabel alloc]init];
        _fansCount.textColor = [UIColor whiteColor];
//        _fansCount.text = @"56粉丝";
        _fansCount.font = FONT(customFontName, 17);
    }
    return _fansCount;
}

-(UILabel *)focusCount{
    if(!_focusCount){
        _focusCount = [[UILabel alloc]init];
        _focusCount.textColor = [UIColor whiteColor];
//        _focusCount.text = @"36关注";
        _focusCount.font = FONT(customFontName, 17);
    }
    return _focusCount;
}

-(UILabel *)favoriteCount{
    if(!_favoriteCount){
        _favoriteCount = [[UILabel alloc]init];
        _favoriteCount.textColor = [UIColor whiteColor];
//        _favoriteCount.text = @"123赞";
        _favoriteCount.font = FONT(customFontName, 17);
    }
    return _favoriteCount;
}

- (RGMeTagView *)sexTag {
    if (!_sexTag) {
        _sexTag = [[RGMeTagView alloc] initWithIcon:IMAGE_NAMED(@"性别-男") WithTitle:@""];
    }
    return _sexTag;
}

- (RGMeTagView *)locationTag {
    if (!_locationTag) {
        _locationTag = [[RGMeTagView alloc] initWithIcon:nil WithTitle:@""];
    }
    return _locationTag;
}

//- (RGMeTagView *)schoolTag {
//    if (!_schoolTag) {
//        _schoolTag = [[RGMeTagView alloc] initWithIcon:nil WithTitle:@""];
//    }
//    return _schoolTag;
//}

-(UILabel *)desLabel{
    if(!_desLabel){
        _desLabel = [[UILabel alloc]init];
        _desLabel.textColor = [UIColor whiteColor];
//        _desLabel.text = @"一句话介绍自己";
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.font = FONT(customFontName, 14);
        _desLabel.numberOfLines = 2;
    }
    return _desLabel;
}



- (UIButton *)addFriendBtn {
    if (!_addFriendBtn) {
        _addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFriendBtn setImage:IMAGE_NAMED(@"发现好友") forState:UIControlStateNormal];
        _addFriendBtn.tag = AddFriend;
        [_addFriendBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addFriendBtn;
}

- (UIButton *)vipBtn {
    if (!_vipBtn) {
        _vipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_vipBtn setImage:IMAGE_NAMED(@"会员") forState:UIControlStateNormal];
        _vipBtn.tag = Vip;
        [_vipBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _vipBtn;
}

- (UIButton *)qRCodeBtn {
    if (!_qRCodeBtn) {
        _qRCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qRCodeBtn setImage:IMAGE_NAMED(@"二维码") forState:UIControlStateNormal];
        _qRCodeBtn.tag = QrCode;
        [_qRCodeBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qRCodeBtn;
}

-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0;
        _backView.userInteractionEnabled = YES;
    }
    return _backView;
}

- (UIView *)btnsView {
    if (!_btnsView) {
        _btnsView = [[UIView alloc] init];
    }
    return _btnsView;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
